package com.link.base.base.applyquality.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.user.model.CoreUser;
import com.link.base.base.applyquality.dao.mybatis.mapper.ApplyQualityMapper;
import com.link.base.base.applyquality.model.ApplyQuality;
import com.link.base.base.applyquality.model.ApplyQualityMeg;
import com.link.base.base.approval.model.Flow;
import com.link.base.base.approval.service.FlowService;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liushoulong
 * @date 2018-11-08
 * @desc 酒体质检报告申请
 */
@Service
public class ApplyQualityServiceImpl extends BasicServiceImpl<ApplyQuality> implements ApplyQualityService {

    @Resource
    private ApplyQualityMapper applyQualityMapper;
    @Resource
    private FlowService flowService;

    @Override
    public BasicMapper<ApplyQuality> getBasicMapper() {
        return applyQualityMapper;
    }

    @Override
    public void insert(ApplyQuality entity) throws Exception {
        CoreUser user = UserUtil.getUser();
        entity.setApplicantId(user.getId());
        entity.setApplicant(user.getFirstName());
        entity.setUserId(user.getId());
        super.insert(entity);
    }

    /**
     * 根据用户查询申请列表
     *
     * @return List
     * @throws Exception
     */
    @Override
    public List<ApplyQuality> queryByUserExamplePage() throws Exception {
        CoreUser user = UserUtil.getUser();
        List<ApplyQuality> applyQualityList = applyQualityMapper.queryByUserExamplePage(user);
        return applyQualityList;
    }

    /**
     * 提交更改信息审批
     *
     * @param applyQuality
     * @param approvalDetail
     * @throws Exception
     */
    @Override
    public void accntSigningSubmit(ApplyQuality applyQuality, String approvalDetail) throws Exception {
        Long selectUserId = 0L;
        String approvalType = "QualityReportExpense";
        //创建审批流程
        flowService.createFlow(applyQuality.getId(), approvalType, selectUserId,
                applyQuality.getAccntName(), applyQuality.getAccntId(), approvalDetail);
        applyQuality.setQualityStatus("auditing");
        applyQualityMapper.update(applyQuality);
    }

    /**
     * 审批通过后更新客户信息及申请信息状态
     *
     * @param objectId
     * @param param1
     * @param param2
     * @param param3
     * @param param4
     * @param param5
     * @throws Exception
     */
    @Override
    public void qualityStatusUpdate(Long objectId, String param1, String param2, String param3, String param4, String param5) throws Exception {
        /*申请信息*/
        String approved = "Approved";
        String refuse = "Refuse";
        CoreUser user = UserUtil.getUser();
        ApplyQuality queryObject = new ApplyQuality();
        queryObject.setId(objectId);
        ApplyQuality entity = queryById(queryObject);
        //审批拒绝
        if (refuse.equals(param1)) {
            //已拒绝
            entity.setQualityStatus(param2);
            applyQualityMapper.update(entity);
        }
        //审批通过
        if (approved.equals(param1)) {
            //审核中
            entity.setQualityStatus(param2);
            applyQualityMapper.update(entity);
        }
    }


    /**
     * 最后节点处理通过后，审批对象修改
     *
     * @param entity
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void approvalObjectUpdate(ApplyQualityMeg entity) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String flag = "Y";
        Flow flow = new Flow();
        flow.setId(entity.getApprovalId());
        Flow flowObj = flowService.queryById(flow);
        if (flag.equals(flowObj.getCurrentIsWrite())) {
            /*审批详情封装json字符串*/
            String approvalDetail = mapper.writeValueAsString(entity);
            /*审批流程更新*/
            flowObj.setFlowObjDetail(approvalDetail);
            flowService.update(flowObj);
        }

        if (StringUtils.isBlank(flowObj.getNewStepNum())) {
            ApplyQuality obj = entity.getApplyQuality();
            getBasicMapper().update(obj);
        }
    }


}
